package controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import models.Trades;

import play.mvc.After;
import play.mvc.Before;
import play.mvc.Controller;
import play.mvc.Scope;

import com.etaoking.commons.Contants;
import com.etaoking.commons.Page;
import com.etaoking.commons.TaobaoUtils;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.TaobaokeItem;
import com.taobao.api.domain.TaobaokeShop;
import com.taobao.api.domain.Trade;
import com.taobao.api.request.TradesBoughtGetRequest;
import com.taobao.api.response.TradesBoughtGetResponse;


public class Application extends Controller {
	
	@After
	public static void putRenderParam(){
		flash.put("menuNo", 0);
	}
	
	@Before
	public static void getParam(){
		
	}
	
    public static void index(int currentPageNo) {
    	if(currentPageNo == 0)currentPageNo = 1;
    	Page page = new Page();
    	page.setCurrentPageNo(currentPageNo);
			
			List<Trades> trades = Trades.findAll();
			page.setData(trades);
			System.out.println(trades.size());
			page.setTotalCount(trades==null?0:trades.size());
			int totalPage = page.getTotalCount()%page.getPageSize()==0?page.getTotalCount()/page.getPageSize():page.getTotalCount()/page.getPageSize()+1;
			page.setPrePageNo((currentPageNo - 1)<1?1:currentPageNo - 1);
	    	page.setNextPageNo((currentPageNo + 1)<totalPage?currentPageNo + 1:totalPage);
			List<String> array = new ArrayList<String>();
			
			if(page.getCurrentPageNo()>6){
				int i = 0;
				if(totalPage > page.getCurrentPageNo()+4)i = page.getCurrentPageNo()+4;
				else if(totalPage < page.getCurrentPageNo()+4)i = totalPage - 1;
				else if(totalPage == page.getCurrentPageNo()+4)i = totalPage;
				array.add("1");
				int start = 2;
				if(totalPage>11)start = page.getCurrentPageNo() - 4;
				for(int j = start;j<=i;j++){
					array.add(j+"");
				}
				array.add(totalPage+"");
			}else{
				int i = 1;
				if(totalPage>11){
					i = 10;
				}else{
					i = totalPage;
				}
				for(int j = 1;j<=i;j++){
					array.add(j+"");
				}
			}
			page.setArray(array);
			for(String key : array){
				System.out.println(key);
			}
			List<TaobaokeItem> recommendItems = TaobaoUtils.getRecommendItems(16L, 0L, 4L);
			List<TaobaokeShop> taobaokeShops = TaobaoUtils.getTaobaokeShops();
			render(trades,page,recommendItems,taobaokeShops);
    }
    
    public static void doc(){
    	render();
    }
}